package com.ssy.wireless.web.controller;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.ssy.wireless.entity.*;
import com.ssy.wireless.util.DateUtils;
import com.ssy.wireless.util.RespVo;
import com.ssy.wireless.web.service.EnterpriseService;
import com.ssy.wireless.web.service.HotspotService;
import com.ssy.wireless.web.vo.LastMonthStatVo;
import com.ssy.wireless.web.vo.req.HotspotQueryVo;
import com.ssy.wireless.web.vo.resp.EchartsBarVo;
import com.ssy.wireless.web.vo.resp.EchartsSeriesVo;
import com.ssy.wireless.web.vo.resp.EchartsVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.*;

import static com.ssy.wireless.util.RespVo.success;

/**
 * Created by cofey on 16/11/20.
 */
@RestController
@RequestMapping("/hotspot")
public class HotspotController extends AbstractController {
    @Autowired
    private HotspotService hotspotService;
    @Autowired
    private EnterpriseService enterpriseService;

    @GetMapping("/")
    public ModelAndView list(@RequestParam(value = "entId", required = false) Long entId) {
        List<LastMonthStat> lastMonthStats = hotspotService.selectLastMonthStat();
        LastMonthStatVo mobStatVo = new LastMonthStatVo();
        LastMonthStatVo telStatVo = new LastMonthStatVo();
        lastMonthStats.stream().forEach(vo -> {
            if (1 == vo.getV2()) {
                switch (vo.getV1()) {
                    case 1:
                        telStatVo.setHotspotCount(vo.getV3().intValue());
                        break;
                    case 2:
                        telStatVo.setNormalCount(vo.getV3().intValue());
                        break;
                    case 3:
                        telStatVo.setFaultCount(vo.getV3().intValue());
                        break;
                    case 4:
                        telStatVo.setJoinCount(vo.getV3().intValue());
                        break;
                    case 5:
                        telStatVo.setUpTraffic(vo.getV3().doubleValue() / 100);
                        telStatVo.setDownTraffic(vo.getV4().doubleValue() / 100);
                        break;
                    default:
                        break;
                }
            } else if (2 == vo.getV2()) {
                Long v3 = vo.getV3();
                switch (vo.getV1()) {
                    case 1:
                        mobStatVo.setHotspotCount(v3.intValue());
                        break;
                    case 2:
                        mobStatVo.setNormalCount(v3.intValue());
                        break;
                    case 3:
                        mobStatVo.setFaultCount(v3.intValue());
                        break;
                    case 4:
                        mobStatVo.setJoinCount(v3.intValue());
                        break;
                    case 5:
                        mobStatVo.setUpTraffic(v3.doubleValue() / 100);
                        mobStatVo.setDownTraffic(vo.getV4().doubleValue() / 100);
                        break;
                    default:
                        break;
                }
            }
        });
        Map<String, Object> result = new HashMap<>();
        result.put("mob", mobStatVo);
        result.put("tel", telStatVo);
        Enterprise enterprise = new Enterprise();
        if (null != entId) {
            enterprise = enterpriseService.findById(entId);
        }
        result.put("ent", enterprise);
        return new ModelAndView("hotspot/list", result);
    }

    @PostMapping("/content")
    public ModelAndView content(
            @RequestParam(value = "pageIndex", defaultValue = "1", required = false) int pageIndex,
            @RequestParam(value = "pageSize", defaultValue = "10", required = false) int pageSize,
            HotspotQueryVo vo
    ) {
        Assert.isTrue(pageSize < 200, "每页显示条数不能大于200");
        Page<Enterprise> page = PageHelper.offsetPage(pageIndex - 1, pageSize);
        hotspotService.find(vo.queryParams());
        return new ModelAndView("hotspot/content", result(page));
    }

    @GetMapping("/statistics/{hotspotType}")
    public ModelAndView statistics(@PathVariable(value = "hotspotType", required = false) Integer hotspotType) {
        return new ModelAndView("hotspot/statistics", Collections.singletonMap("hotspotType", hotspotType));
    }

    @GetMapping("/all/{hotspotType}")
    public List<Hotspot> getHpAll(@PathVariable(value = "hotspotType", required = false) Integer hotspotType) {
        return hotspotService.find(Collections.singletonMap("type", null != hotspotType && hotspotType.intValue() > 0 ? hotspotType : null));
    }

    @PostMapping("/statistics/online/content")
    public RespVo<EchartsVo> statisticsOnlineContent(@RequestParam(value = "type", defaultValue = "1", required = false) int type,
                                                     @RequestParam(value = "beginDate", required = false) String beginDate,
                                                     @RequestParam(value = "endDate", required = false) String endDate,
                                                     @RequestParam(value = "hotspotType", defaultValue = "2") Integer hotspotType) {
        xAxisAndBeginAndEndDate abe = setxAxisAndBeginAndEndDate(type, beginDate, endDate);
        List<String> xAxis = abe.getxAxis();
        Map<String, Object> params = new HashMap();
        params.put("type", type);
        params.put("begin", abe.getBegin());
        params.put("end", abe.getEnd());
        params.put("hotspotType", hotspotType.intValue() == 0 ? null : hotspotType);
        List<String> series = new ArrayList();
        List<HotspotOnlineStat> statistics = hotspotService.findOnlineStatistics(params);
        for (String date : xAxis) {
            series.add(setOnlineDataItem(type, date, statistics));
        }
        return success(EchartsVo.build().createTitle("热点在线统计").createxAxis(xAxis).createSeries(series, "", "line"));
    }

    private String setOnlineDataItem(int type, String date, List<HotspotOnlineStat> stats) {
        String val = "0";
        for (HotspotOnlineStat hts : stats) {
            String dateVal = date.replaceAll("-", "");
            switch (type) {
                case 1:
                    dateVal = DateUtils.format(DateUtils.getTime(), "yyyyMMdd") + date;
                    val = dateVal.equals(hts.getHourVal().toString()) ? hts.getOnlineCount().toString() : "0";
                    break;
                case 2:
                    val = hts.getDayVal().toString().equals(dateVal) ? hts.getOnlineCount().toString() : "0";
                    break;
                case 3:

                    break;
                case 4:
                    val = hts.getMonthVal().toString().equals(dateVal) ? hts.getOnlineCount().toString() : "0";
                    break;
                default:
                    val = hts.getYearVal().toString().equals(date) ? hts.getOnlineCount().toString() : "0";
                    break;
            }
            if (!"0".equals(val)) break;
        }
        return val;
    }

    @PostMapping("/statistics/traffic/content")
    public RespVo<EchartsBarVo> statisticsTrafficContent(@RequestParam(value = "type", defaultValue = "1", required = false) int type,
                                                         @RequestParam(value = "beginDate", required = false) String beginDate,
                                                         @RequestParam(value = "endDate", required = false) String endDate,
                                                         @RequestParam(value = "hotspotId", defaultValue = "0", required = false) Long hotspotId,
                                                         @RequestParam(value = "hotspotType", defaultValue = "2") Integer hotspotType) {
        List<String> legends = new ArrayList();
        legends.add("上行流量");
        legends.add("下行流量");
        xAxisAndBeginAndEndDate abe = setxAxisAndBeginAndEndDate(type, beginDate, endDate);
        List<String> xAxis = abe.getxAxis();
        List<EchartsSeriesVo> series = new ArrayList();
        Map<String, Object> params = new HashMap();
        params.put("type", type);
        params.put("begin", abe.getBegin());
        params.put("end", abe.getEnd());
        params.put("hotspotId", null != hotspotId && hotspotId.longValue() > 0l ? hotspotId : null);
        params.put("hotspotType", hotspotType.intValue() == 0 ? null : hotspotType);
        List<HotspotTrafficStat> statistics = hotspotService.findTrafficStatistics(params);
        for (int i = 0; i < legends.size(); i++) {
            EchartsSeriesVo vo = new EchartsSeriesVo();
            vo.setName(legends.get(i));
            List<Double> data = new ArrayList();
            for (String date : xAxis) {
                data.add(setTrafficDataItem(type, legends.get(i), date, statistics));
            }
            vo.setData(data);
            series.add(vo);
        }
        return success(EchartsBarVo.build().createTitle("热点流量统计").createLegend(legends).createxAxis(xAxis).createSeries(series));
    }

    private Double setTrafficDataItem(int type, String legend, String date, List<HotspotTrafficStat> stats) {
        double val = 0;
        for (HotspotTrafficStat hts : stats) {
            String dateVal = date.replaceAll("-", "");
            switch (type) {
                case 1:
                    dateVal = DateUtils.format(DateUtils.getTime(), "yyyyMMdd") + date;
                    val = dateVal.equals(hts.getHourVal().toString()) ? getVal(legend, hts) : 0;
                    break;
                case 2:
                    val = hts.getDayVal().toString().equals(dateVal) ? getVal(legend, hts) : 0;
                    break;
                case 3:

                    break;
                case 4:
                    val = hts.getMonthVal().toString().equals(dateVal) ? getVal(legend, hts) : 0;
                    break;
                default:
                    val = hts.getYearVal().toString().equals(date) ? getVal(legend, hts) : 0;
                    break;
            }
            if (val > 0) break;
        }
        return val;
    }

    private Double getVal(String legend, HotspotTrafficStat mas) {
        if (legend.equals("上行流量")) {
            return mas.getUpTraffic();
        } else {
            return mas.getDownTraffic();
        }
    }
}
